﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Admin_BookDetailManage : System.Web.UI.Page
{
    public string orderOftype;
    public string imageurl;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlDataSource1.SelectCommand = "select Book_info.BookID as 图书编号,Book_info.BookName as 图书条目,Book_info.FindBookNum as 索书号,Book_info.Writer as 作者,Book_info.BookPlace as 馆藏地点, book_type.TypeName as 类型名称, price as 价格,PublishCompanyName as 出版社,Amount as 图书总数,CurrentNum as 在库数,BuyDate as 购买日期,InDate as 收录日期  from Book_Type,Book_info ,Publish_company where Publish_company.PublishcompanyID = book_info.PublishcompanyID and Book_Type.BookType=Book_info.BookType ";
       


    }
    protected void delete_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in mybookdata.Rows)
        {
            Control ctrl = row.FindControl("CheckBox");

            if ((ctrl as CheckBox).Checked)
            {
                TableCellCollection cell = row.Cells;
                string typeid = cell[1].Text;
                SQL sql = new SQL();
                sql.EXQSQL("delete from book_info where bookid='" + typeid + "'");
            }
        }
        Response.Write("<script language=javascript>alert('删除成功');window.location = 'BookDetailManage.aspx';</script>");
    }

    protected void change_Click(object sender, EventArgs e)
    {
        try
        {
            int i = Convert.ToInt32(TextBox1.Text);
            SQL sql = new SQL();
         
            sql.EXQSQL("update book_info set bookid='" + TextBox1.Text + "' ,bookname='" + TextBox2.Text + "',writer='" + TextBox6.Text + "',publishdate='" + TextBox8.Text + "',price='" + TextBox16.Text + "',amount='" + TextBox18.Text + "',currentnum='" + TextBox19.Text + "',buydate='" + TextBox21.Text + "',indate='" + TextBox22.Text + "',isbn='" + TextBox23.Text + "',BookPlace='" + TextBox24.Text + "',FindBookNum='" + TextBox25.Text + "',MULU='" + TextBox27.Text + "',ContentAbstract='" + TextBox26.Text + "',imageURL='" + Image2.ImageUrl + "',publishCompanyID ='" + DropDownList3.SelectedIndex.ToString() + "',Booktype='" + DropDownList4.SelectedIndex.ToString() + "' where  bookid='" + TextBox1.Text + "'");




            Response.Write("<script language=javascript>alert('修改成功');window.location = 'BookDetailManage.aspx';</script>");
        }
        catch (System.Exception ex)
        {
            Label1.Text = "警告：您输入有误";
        }
    }

    protected void chaxun_Click(object sender, EventArgs e)
    {

    }
    protected void sureAdd_Click(object sender, EventArgs e)
    {
        string bookid = TypeID.Text;
        string bookname = TextBox3.Text;
        string writer = TextBox4.Text;
        string publishdate = TextBox5.Text;
        string  booktype= DropDownList1.SelectedIndex.ToString();
        string price = TextBox7.Text;
        string publishCompany = DropDownList2.SelectedIndex.ToString();
        string amount = TextBox9.Text;
        string currentnum = TextBox10.Text;
        string buydate = TextBox11.Text;
        string indaye = TextBox12.Text;
        string isbn = TextBox13.Text;
        string place = TextBox14. Text;
        string findbooknum = TextBox15.Text;
        string atc = TextBox17.Text;
        string content = TextBox20.Text;
        imageurl = FileUpload1.FileName.ToString();
        Image1.ImageUrl = imageurl;
        

        try
        {
           
            SQL sql = new SQL();
            sql.EXQSQL("insert into book_info values('" + bookid + "','" + bookname + "','" + atc + "','" + writer + "','" + booktype + "','" + price + "','" + publishCompany + "','" + publishdate + "','" + amount + "','" + currentnum + "','" + buydate + "','" + indaye + "','" + isbn + "','1',NULL,'0','" + place + "','" + findbooknum + "','" + imageurl + "','" + content + "') ");
            
            Response.Write("<script language=javascript>alert('增加成功');window.location = 'BookDetailManage.aspx';</script>");
        }
        catch (System.Exception ex)
        {
            Label1.Text = "警告：您输入的有误,请检查重新输入";
        }



    }



    protected void Match_Click(object sender, EventArgs e)
    {

        string bookname = TextBox3.Text;
        UpdataMybookdata UP = new UpdataMybookdata();
        string html=UP.GetHTML("https://api.douban.com/v2/book/search?q=" + bookname + "&count=1");
        var data = UpdataMybookdata.ScriptDeserialize(html);
        TextBox3.Text = data.books[0].title;
        string writer = TextBox4.Text=data.books[0].author[0];
        string publishdate = TextBox5.Text = data.books[0].pubdate ;
        string price = TextBox7.Text=data.books[0].price;
        string isbn = TextBox13.Text=data.books[0].isbn13;
        string atc = TextBox17.Text=data.books[0].summary;
        string content = TextBox20.Text = data.books[0].catalog;
        Image1.ImageUrl = data.books[0].images.large;

    }
    protected void flash_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in mybookdata.Rows)
        {
            Control ctrl = row.FindControl("CheckBox");

            if ((ctrl as CheckBox).Checked)
            {
                TableCellCollection cell = row.Cells;
                string typeid = cell[1].Text;
                SQL sql = new SQL();



                DataSet ds = sql.getDataset("select * from book_info,Book_status,Book_type,Publish_company where Book_info.StatusID=Book_status.StatusID and  book_info.PublishCompanyID=Publish_company.PublishCompanyID and Book_info.BookType=Book_Type.BookType and  BookID='" + typeid + "'", "");
                DataTable dt = new DataTable();
                DataRow drow;
                DataRowCollection drc;
                dt = ds.Tables[0];
                drc = dt.Rows;
                for (int i = 0; i < drc.Count; i++)
                {
                    drow = drc[i];
                    string bookT = drow[0].ToString();
                    string bookname = drow[1].ToString();
                    string atr = drow[2].ToString();
                    string write = drow[3].ToString();

                    string price = drow[5].ToString();

                    string publishdate = drow[7].ToString();
                    publishdate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(publishdate));
                    string nownum = drow[9].ToString();
                    string amount = drow[8].ToString();

                    string indate = string.Format("{0:yyyy-MM-dd}", DateTime.Parse(drow[11].ToString()));
                    string isbn = drow[12].ToString();
                    string beborrow = drow[15].ToString();
                    string bookplace = drow[16].ToString();
                    string findbooknum = drow[17].ToString();
                    string imageURL = drow[18].ToString();
                    string mulu = drow[19].ToString();
                    string status = drow[21].ToString();
                    int publishCompanyID = Convert.ToInt32(drow[22].ToString());
                    string bookTypename = drow[23].ToString();
                    int mytypeid = Convert.ToInt32(drow[20].ToString());
                    string buydate1 = drow[10].ToString();
                    string place1 = drow[16].ToString();

                    string bookid = TextBox1.Text = bookT;
                    bookname = TextBox2.Text = bookname;
                    string writer = TextBox6.Text = write;
                    publishdate = TextBox8.Text = publishdate;
                    DropDownList3.SelectedIndex = mytypeid;
                    price = TextBox16.Text = price;
                    DropDownList4.SelectedIndex = publishCompanyID;
                    amount = TextBox18.Text = amount;
                    string currentnum = TextBox19.Text = nownum;
                    string buydate = TextBox21.Text = buydate1;
                    string indaye = TextBox22.Text = indate;
                    isbn = TextBox23.Text = isbn;
                    string place = TextBox24.Text = place1;
                    findbooknum = TextBox25.Text = drow[17].ToString();
                    string atc = TextBox26.Text = atr;
                    string content = TextBox27.Text = mulu;
                    imageurl = FileUpload1.FileName.ToString();
                    Image2.ImageUrl = imageURL;
                }


            }

        }
    }
}